var mongoose=require('mongoose');
var Schema=mongoose.Schema;
var config=require('../config');
var User1=require('./user')
var Calendar=require('./calendar')
var Coffee=require('./coffee')
var db=mongoose.createConnection(config.db_url);
db.on('error',function(error){
    console.log(error);
});

db.model('user',User1.userSchema);
db.model('calendar',Calendar.calendarSchema);
db.model('coffee',Coffee.coffeeSchema);
var orderRecordSchema=new Schema({
    orderMessage:String,
    orderDate:{type:Date,default:Date.now},
    orderUser:{type:Schema.Types.ObjectId,ref:'user'},
    orderToUser:{type:Schema.Types.ObjectId,ref:'user'},
    orderCalendar:{type:Schema.Types.ObjectId,ref:'calendar'},
    orderCoffee:{type:Schema.Types.ObjectId,ref:'coffee'},
    state:{type:Number,default:0}
});
//users---对应数据库表名
var OrderRecord=db.model('OrderRecord',orderRecordSchema);

/**
 * 获得简介信息
 */

OrderRecord.prototype.findAllOrderRecord=function(callback){
    OrderRecord.find({}).populate('orderToUser orderCalendar orderCoffee orderUser').limit(3).sort('-orderDate').exec(function(err,datas){
        callback(err,datas);
    })
}

OrderRecord.prototype.updateOrderRecordById=function(id,options,callback){
    OrderRecord.update({_id:id},options,function(err){
        callback(err)
    });
}
OrderRecord.prototype.getNewOrderRecord=function(options,callback){
    OrderRecord.find(options).populate('orderToUser orderCalendar orderCoffee orderUser').limit(3).sort('-orderDate').exec(function(err,datas){
        callback(err,datas);
    })
}
OrderRecord.prototype.findOrderRecordById=function(id,callback){
    OrderRecord.findOne({_id:id},function(err,data){
        callback(err,data);
    });
}
OrderRecord.prototype.findOrderRecordByOptions=function(options,callback){
    OrderRecord.findOne(options,function(err,data){
        callback(err,data);
    });
}
OrderRecord.prototype.delOrderRecordBySid=function(sid,callback){
    OrderRecord.remove({orderCalendar:sid},function(err){
        callback(err);
    })
}
OrderRecord.prototype.changState=function(id,state,callback){
    OrderRecord.update({_id:id},{state:state},function(err){
        callback(err);
    })
}

OrderRecord.prototype.findForPage=function(obj,callback){
    var q=obj.search||{};                                       //查询条件
    var col=obj.columns||{};                                    //返回字段
    var query=OrderRecord.find(q,col).skip(obj.page.skipFrom).limit(obj.page.limit).sort('-orderDate').populate('orderCalendar orderToUser orderUser orderCoffee');
    query.exec(function(err, datas) {
        if (err) {
            callback(err, null, null);
        } else {
            OrderRecord.count(q, function(err, count) {
                if (err) {
                    callback(err, null, null);
                } else {
                    var pageCount = Math.ceil(count / obj.page.limit);
                    callback(null, count,pageCount, datas);
                }
            });
        }
    });

}
module.exports =OrderRecord;
module.exports.orderRecordSchema=orderRecordSchema;


